/*	IntegerFieldFormItem.java
 *
 *	Copyright (c) Christopher Jacobs 2010
 *
 *	This file is part of MSGToolSuite_Swing ('this project').
 *
 *	This project is free software: you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the Free
 *	Software Foundation, either version 3 of the License, or (at your option)
 *	any later version.
 *
 *	This project is distributed in the hope that it will be useful, but WITHOUT
 *	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *	FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 *	more details.
 *
 *	You should have received a copy of the GNU General Public License along with
 *	this project. If not, see <http://www.gnu.org/licenses/>.
 */
package edu.bu.manateem.msg.gui.component;

import java.text.NumberFormat;

import javax.swing.JFormattedTextField;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *	<p>
 *	
 *	</p>
 *
 *
 *	@author
 *		Christopher Jacobs
 *
 *	@version
 *		1.0
 *
 *	@changelog
 *		May 21, 2010	- Version 1.0.
 */
public class IntegerFieldFormItem
	extends FormattedFieldFormItem
{
	/*
	 * TYPES
	 */

	/*
	 * CONSTANTS
	 */

	// Serialize
	private static final long serialVersionUID = 1L;
	// Logging
	private static final Logger LOG =
		LoggerFactory.getLogger(IntegerFieldFormItem.class);
	
	/*
	 * FIELDS
	 */

	/*
	 * CONSTRUCTORS
	 */

	public IntegerFieldFormItem(String label,String units,int maxDigits)
	{
		super(label,units);
		
		((NumberFormat) format).setMaximumIntegerDigits(maxDigits);
	}
	
	/*
	 * INITIALIZERS
	 */

	/*
	 * METHODS
	 */

	@Override
	public Integer getValue()
	{
		Integer value = (Integer) ((JFormattedTextField) item).getValue();
		if (value == null)
		{
			return new Integer(-1);
		}
		return new Integer(value.intValue());
	}
	
	/**
	 *	<p>
	 *	
	 *	</p>
	 *
	 *	@since
	 *		
	 */
	@Override
	protected void initFormat()
	{
		IntegerFieldFormItem.LOG.debug("init format item as integer format");
		
		// build format
		NumberFormat integerFormat = NumberFormat.getIntegerInstance();
		format = integerFormat;
	}
	
	@Override
	protected void initItem()
	{
		IntegerFieldFormItem.LOG.debug("init form item as formatted text field");
		
		// build format
		initFormat();
		
		// build item
		JFormattedTextField formItem = new JFormattedTextField(format);
		item = formItem;
	}
	
	public void setUnits(String units)
	{
		setDescription(units);
	}
}
